#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkcdn.request.v20180510.VerifyDomainOwnerRequest import VerifyDomainOwnerRequest
from aliyunsdkcdn.request.v20180510.DescribeVerifyContentRequest import DescribeVerifyContentRequest
from aliyunsdkcdn.request.v20180510.AddCdnDomainRequest import AddCdnDomainRequest
from aliyunsdkcdn.request.v20180510.BatchSetCdnDomainServerCertificateRequest import BatchSetCdnDomainServerCertificateRequest
from aliyunsdkcdn.request.v20180510.DescribeCdnDomainDetailRequest import DescribeCdnDomainDetailRequest

#cdn检查域名所属，校验
def checkDomain(key,secret,domain):
    credentials = AccessKeyCredential(key, secret)
    # use STS Token
    # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
    client = AcsClient(region_id='cn-shanghai', credential=credentials)
    request = VerifyDomainOwnerRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain)
    request.set_VerifyType("dnsCheck")
    response = client.do_action_with_exception(request)
    # python2:  print(response) 
    print('【信息】校验成功！')
    print(str(response, encoding='utf-8'))

#cdn获取域名校验所属，解析值。
def getCheckValue(key,secret,domain):
    credentials = AccessKeyCredential(key, secret)
    # use STS Token
    # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
    client = AcsClient(region_id='cn-shanghai', credential=credentials)
    request = DescribeVerifyContentRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain)
    response = client.do_action_with_exception(request)
    # python2:  print(response) 
    print(str(response, encoding='utf-8'))
    return str(response, encoding='utf-8')


#创建cdn加速域名
def addCdnDomain(key,secret,cdn_type,domain_name,sources):
    credentials = AccessKeyCredential(key, secret)
    # use STS Token
    # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
    client = AcsClient(region_id='cn-hangzhou', credential=credentials)
    request = AddCdnDomainRequest()
    request.set_accept_format('json')
    request.set_CdnType(cdn_type)
    request.set_DomainName(domain_name)
    request.set_Sources(sources)
    response = client.do_action_with_exception(request)
    # python2:  print(response) 
    print(str(response, encoding='utf-8'))

#批量设置域名证书功能
def BatchSetCdnDomainServerCertificate(key,secret,domain_name,sslpri,sslpub):
    credentials = AccessKeyCredential(key, secret)
    # use STS Token
    # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
    client = AcsClient(region_id='cn-hangzhou', credential=credentials)
    request = BatchSetCdnDomainServerCertificateRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain_name)
    request.set_SSLProtocol("on")
    request.set_SSLPri(sslpri)
    request.set_SSLPub(sslpub)
    response = client.do_action_with_exception(request)
    # python2:  print(response) 
    print(str(response, encoding='utf-8'))

def DescribeCdnDomainDetail(key,secret,domain_name):
    credentials = AccessKeyCredential(key, secret)
    # use STS Token
    # credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
    client = AcsClient(region_id='cn-hangzhou', credential=credentials)
    request = DescribeCdnDomainDetailRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain_name)
    response = client.do_action_with_exception(request)
    # python2:  print(response) 
    print(str(response, encoding='utf-8'))
    return str(response, encoding='utf-8')